<?php
/**
 * 推荐位
 *
 * @copyright  Copyright (c) 中国追溯
 * @since      File available since Release v1.1
 */

defined('InShopNC') or exit('Access Invalid!');

class rec_positionModel extends Model
{
    private $rec_channel;   //投放渠道
    private $rec_location;  //投放位置

    public function __construct()
    {
        parent::__construct('rec_position');
        //投放渠道
        $this->rec_channel = array(
            array('channel_id' => 1, 'channel_name'=>'pc端'),
            array('channel_id' => 2, 'channel_name'=>'wap端'),
            array('channel_id' => 3, 'channel_name'=>'微信小程序')
        );
        //投放位置
        $this->rec_location = array(
            array('location_id' => 1, 'channel_id' => 1, 'location_name' => '推荐店铺1'),
            array('location_id' => 2, 'channel_id' => 2, 'location_name' => '推荐店铺2'),
            array('location_id' => 3, 'channel_id' => 3, 'location_name' => '推荐店铺3')
        );
    }

    /**
     * 活动投放渠道
     * @param  boolean $if_dispose 是否处理返回数据
     * @return array
     */
    public function getRecChannel($if_dispose = true)
    {
        $rec_channel = $this->rec_channel;
        return ($if_dispose === true) ? array_column($rec_channel, 'channel_name', 'channel_id') : $rec_channel;
    }

    /**
     * 活动投放位置
     * @param  boolean $if_dispose 是否处理返回数据
     * @param  int $channel_id 投放渠道id （若该参数不为0，则强制将$if_dispose设置为true）
     * @return array
     */
    public function getRecLocation($if_dispose = true, $channel_id = 0)
    {
        $channel_id = (int)$channel_id;
        $if_dispose = $channel_id ? true : $if_dispose;
        $rec_location = $this->rec_location;
        if ($if_dispose !== true) $rec_location;
        $dispose_data = array();    //存放处理后的数据
        $rec_channel = $this->getRecChannel(true);  //投放渠道
        array_walk($rec_location, function($item, $keys)use(&$dispose_data, $rec_channel, $channel_id) {
            $item_channel_id = isset($item['channel_id']) ? (int)$item['channel_id'] : 0;
            $location_id = isset($item['location_id']) ? (int)$item['location_id'] : 0;
            if ($channel_id) { //找出渠道id匹配的数据
                if ($item_channel_id === $channel_id) {
                    $dispose_data[$location_id] = $item;
                }

            } else {
                if(!isset($dispose_data['parent'][$item_channel_id]) && isset($rec_channel[$item_channel_id])) {
                    $dispose_data['parent'][$item_channel_id] = $rec_channel[$item_channel_id];
                }
                if (!isset($dispose_data['tier'][$item_channel_id][$location_id])) {
                    $dispose_data['tier'][$item_channel_id][$location_id] = $item;
                }
            }
        });
        return $dispose_data;
    }    

}
